import { Component, forwardRef, Input } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
import { TradeAmountMaxValue } from 'app/utils/contraints'

@Component({
  selector: 'app-trade-amount-input',
  templateUrl: './trade-amount.component.html',
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => TradeAmountInputComponent),
    multi: true
  }]
})
export class TradeAmountInputComponent implements ControlValueAccessor {

  TradeAmountMaxValue = TradeAmountMaxValue

  value: number

  // 交易金额的精度(decimal)
  @Input() amountPrecision = 2

  onChange: (value: number) => void = (value: number) => { }
  onTouched: () => void = () => { }

  updateChanges() {
    this.onChange(this.value)
  }

  writeValue(value: number): void {
    this.value = value
    this.updateChanges()
  }

  registerOnChange(fn: any): void {
    this.onChange = fn
  }

  registerOnTouched(fn: any): void {
    this.onTouched = fn
  }

}
